<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_img">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登录表单 -->
      <el-form
        ref="loginFormref"
        class="login_form"
        :model="loginForm"
        :rules="loginUserRules"
        ><!--ref属性为获取表单实例对象-->
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <!-- 密码数据校验 -->
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button type="info" @click="resetloginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      //登场数据绑定
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //数据校验
      loginUserRules: {
        //用户校验规则
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        //密码校验规则
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //重置文本框
    resetloginForm() {
      this.$refs.loginFormref.resetFields();
    },
    //提交验证
    submitLoginForm() {
      this.$refs.loginFormref.validate(async (vaild) => {
        if (!vaild) return;
        //向api接口提交一个请求， 状态码200 为登录成功 $message为el组件
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status !== 200) return this.$message.error("登录失败");
        this.$message.success("登录成功");
        //将登录之后的token保存在客户端的sessionStorage中
        // 项目中除了登录之外的其他api接口，必须在登录之后才可以访问
        //token只应该在当前网站打开期间生效，所以将token保存在sessionStorage
        console.log(res);
        window.sessionStorage.setItem("token", res.data.token);
        //然后页面跳转 ，到/home
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}
.login_img {
  position: absolute;
  left: 50%;
  top: -25%;
  transform: translateX(-50%);
  width: 130px;
  height: 130px;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 5px;
  border-radius: 50%;
  box-shadow: 0 0 10px #ddd;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #ccc;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>